package com.frxuexi.myroom.view

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import androidx.paging.compose.collectAsLazyPagingItems
import androidx.paging.compose.items
import com.frxuexi.lib_base.R
import com.frxuexi.lib_base.RoutePath
import com.frxuexi.lib_base.UiState
import com.frxuexi.lib_base.room.Leave
import com.frxuexi.myroom.viewmodel.LeaveViewModel
import com.google.gson.Gson
import com.tencent.mmkv.MMKV
import kotlinx.coroutines.launch

/**
 * 请假列表页面
 */
@Composable
fun RoomLeaveListPage(navHostController: NavHostController,vm: LeaveViewModel = hiltViewModel()) {

    val uid = MMKV.defaultMMKV().decodeInt("uid", -1)
    val list = vm.getDatas(uid).collectAsLazyPagingItems()


    Column(
        Modifier
            .fillMaxSize()
            .padding(20.dp), verticalArrangement = Arrangement.spacedBy(10.dp)) {
        Row (
            Modifier
                .fillMaxWidth()
                , horizontalArrangement = Arrangement.SpaceBetween){
            Icon(imageVector = Icons.Default.ArrowBack, contentDescription = "",Modifier.clickable { 
                navHostController.popBackStack()
            })
            Text(text = "请假记录")
            Text(text = "申请",Modifier.clickable {
                navHostController.navigate(RoutePath.ROOMLEAVE)
            })
        }
        Row(
            Modifier
                .fillMaxWidth()
                .background(colorResource(id = R.color.lightblue))
                .padding(vertical = 15.dp)
            , horizontalArrangement = Arrangement.SpaceAround) {
            Text(text = "类型")
            Text(text = "开始时间")
            Text(text = "结束时间")
            Text(text = "审批状态")
        }
        LazyColumn(verticalArrangement = Arrangement.spacedBy(10.dp), content = {
            items(list){
                LeaveItem(entity = it,navHostController)
            }
        })
    }
}

@Composable
fun LeaveItem(entity: Leave?, navHostController: NavHostController){
    Row(
        Modifier
            .fillMaxWidth()
            .background(Color.LightGray, RoundedCornerShape(8.dp))
            .padding(vertical = 15.dp)
            .clickable {
                val json = Gson().toJson(entity)
                navHostController.navigate(RoutePath.LEAVEDETAIL+"/$json")
            }
        , horizontalArrangement = Arrangement.SpaceAround) {
        Text(text = entity?.type!!)
        Text(text = entity.startTime?.split(" ")!![0])
        Text(text = entity.endTime?.split(" ")!![0])

        var color = if(entity.status=="通过") Color.Green else if(entity.status=="拒绝") Color.Red else Color.Gray

        Text(text = entity.status!! ,Modifier.background(color, RoundedCornerShape(5.dp)).padding(10.dp,3.dp))
    }
}
